package com.xzk.ExpressInnWeb.web.filter;

import com.xzk.ExpressInnWeb.util.AdminUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "AccessControlFilter", value = {"/admin/index.html","/admin/views/*","/express/*"})
public class AccessControlFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        if (AdminUtil.getAdminName(req.getSession()) != null) {
            chain.doFilter(request,response);
        } else {
            resp.sendError(404,"权限不足，无法访问！");
        }
    }

    @Override
    public void destroy() {

    }
}
